package org.jeecg.prod.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.client.BizResponse;
import org.jeecg.client.SendUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CudUtils;
import org.jeecg.prod.dto.MesWoSrcDTO;
import org.jeecg.prod.entity.MesWoSrc;
import org.jeecg.prod.service.IMesWoSrcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 工单管理
 * @Author: LiCY
 * @Date: 2023-06-16
 * @Version: V1.0
 */
@Api(tags = "工单管理")
@RestController
@RequestMapping("/prod/mesWoSrc")
@Slf4j
public class MesWoSrcController extends JeecgController<MesWoSrc, IMesWoSrcService> {
    @Autowired
    private IMesWoSrcService mesWoSrcService;

    /**
     * 分页列表查询666
     *
     * @param mesWoSrc
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "工单管理-分页列表查询")
    @ApiOperation(value = "工单管理-分页列表查询", notes = "工单管理-分页列表查询")
    @GetMapping(value = "/list")
    @DS("business")
    public Result<IPage<MesWoSrcDTO>> queryPageList(MesWoSrcDTO mesWoSrc,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        Page<MesWoSrc> page = new Page<MesWoSrc>(pageNo, pageSize);
        IPage<MesWoSrcDTO> pageList = mesWoSrcService.queryPageList(page, mesWoSrc);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mesWoSrc
     * @return
     */
    @AutoLog(value = "工单管理-添加")
    @ApiOperation(value = "工单管理-添加", notes = "工单管理-添加")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_wo_src:add")
    @PostMapping(value = "/add")
    @DS("business")
    public Result<String> add(@RequestBody MesWoSrc mesWoSrc){
        //mesWoSrcService.save(mesWoSrc);
        mesWoSrc.setCtime(new Date());
        Map<String, String> cudMap = CudUtils.getCudMap("create", "mes_wo_src", mesWoSrc);
        try {
            BizResponse bizResponse = SendUtils.sendRunCmd(cudMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesWoSrc
     * @return
     */
    @AutoLog(value = "工单管理-编辑")
    @ApiOperation(value = "工单管理-编辑", notes = "工单管理-编辑")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_wo_src:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("business")
    public Result<String> edit(@RequestBody MesWoSrc mesWoSrc) {
        mesWoSrcService.updateByMultiId(mesWoSrc);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过选中对象删除
     *
     * @param mesWoSrc
     * @return
     */
    @AutoLog(value = "工单管理-通过id删除")
    @ApiOperation(value = "工单管理-通过id删除", notes = "工单管理-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_wo_src:delete")
    @RequestMapping(value = "/delete")
    public Result<String> delete(@RequestBody MesWoSrc mesWoSrc) {
        mesWoSrcService.deleteByMultiId(mesWoSrc);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "工单管理-批量删除")
    @ApiOperation(value = "工单管理-批量删除", notes = "工单管理-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_wo_src:deleteBatch")
    @RequestMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestBody List<MesWoSrc> list) {
        list.forEach(item -> {
            mesWoSrcService.removeById(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "工单管理-通过id查询")
    @ApiOperation(value = "工单管理-通过id查询", notes = "工单管理-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("history")
    public Result<MesWoSrc> queryById(@RequestParam(name = "id", required = true) String id) {
        MesWoSrc mesWoSrc = mesWoSrcService.getById(id);
        if (mesWoSrc == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mesWoSrc);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mesWoSrc
     */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_wo_src:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("history")
    public ModelAndView exportXls(HttpServletRequest request, MesWoSrc mesWoSrc) {
        return super.exportXls(request, mesWoSrc, MesWoSrc.class, "工单管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("mes_wo_src:importExcel")
    @DS("business")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MesWoSrc.class);
    }

}
